import React from 'react';
import { request, useModel } from 'umi';
/*** 品牌管理接口 */

/** 获取品牌列表 */
export async function getBrandList(
  params: {
    current?: number;
    pageSize?: number;
    flag?: number;
    key_words?: number;
  }
) {
  return request(`/api/inventory/brand/${params.current}/limit/${params.pageSize}`, {
    method: 'GET',
    headers: {
      Action_Id: '200202000'
    },
    params: {
      flag: params.flag,
      key_words: params.key_words
    }
  });
}

/** 获取品牌列表(无需权限) */
export async function getBrandListByPage(
  params: {
    current?: number;
    pageSize?: number;
  }
) {
  return request(`/api/inventory/brand/page/${params.current}/limit/${params.pageSize}`, {
    method: 'GET'
  });
}

/** 新增品牌 */
export async function doCreateBrand(data: STOCKGOODSAPI.BrandItem) {
  return request('/api/inventory/brand', {
    method: 'POST',
    headers: {
      Action_Id: '200202001'
    },
    data
  });
}

/** 批量删除品牌 */
export async function deleteBrandBatch(ids: string[]) {
  return request('/api/inventory/brand/batch', {
    method: 'POST',
    headers: {
      Action_Id: '200202002'
    },
    data: {
      brand_ids: ids
    }
  });
}

/** 检查品牌是否存在 */
export async function checkBrandExist(params: any) {
  return request('/api/inventory/brand/exist', {
    method: 'GET',
    params
  });
}

/** 单个删除品牌 */
export async function deleteBrand(brand_id: string) {
  return request(`/api/inventory/brand/${brand_id}`, {
    method: 'DELETE',
    headers: {
      Action_Id: '200202005'
    }
  });
}

/** 更新品牌 */
export async function updateBrand(data: STOCKGOODSAPI.BrandItem) {
  return request(`/api/inventory/brand/${data.brand_id}`, {
    method: 'PATCH',
    headers: {
      Action_Id: '200202004'
    },
    data
  });
}


/*** 库存管理接口 */

/** 获取商品列表 */
export async function getGoodsList(
  params: {
    current?: number;
    pageSize?: number;
    brand_id?: string;
  }) {
  return request(`/api/inventory/goods/${params.current}/limit/${params.pageSize}`, {
    method: 'POST',
    headers: {
      Action_Id: '200200000'
    },
    data: params
  });
}

// 商品信息-下载(200200006)
export async function downGoodsList(
  params: {
    current?: number;
    pageSize?: number;
    brand_id?: string;
  }) {
  return request(`/api/inventory/goods/report`, {
    method: 'POST',
    headers: {
      Action_Id: '200200006'
    },
    data: params
  });
}

/** 获取商品列表（无需权限） */
export async function getGoodsListByPage(
  params: {
    current?: number;
    pageSize?: number;
    brand_id?: string;
    class_id?: string;
    scope?: string;
    goods_status?: string;
  }) {
  return request(`/api/inventory/goods/page/${params.current}/limit/${params.pageSize}`, {
    method: 'POST',
    data: {
      ...params,
      goods_status: params.goods_status === 'all' ? null : 'O',
    }
  });
}

/** 商品编码是否存在 */
export async function checkGoodsIsExist(params: { goods_id: string }) {
  return request(`/api/inventory/goods/${params.goods_id}/exist`, {
    method: 'GET'
  });
}

/** 新增商品 */
export async function doCreateGoods(data: STOCKGOODSAPI.GoodsItem) {
  return request('/api/inventory/goods', {
    method: 'POST',
    headers: {
      Action_Id: '200200001'
    },
    data
  });
}

/** 删除商品 */
export async function deleteGoods(goods_id: string) {
  return request(`/api/inventory/goods/${goods_id}`, {
    method: 'DELETE',
    headers: {
      Action_Id: '200200005'
    }
  });
}

/** 获取商品详情 */
export async function getGoodsDetail(id: string) {
  return request(`/api/inventory/goods/${id}`, {
    method: 'GET',
    headers: {
      Action_Id: '200200003'
    }
  });
}

/** 更新商品 */
export async function updateGoods(data: STOCKGOODSAPI.GoodsItem) {
  return request(`/api/inventory/goods/${data.goods_id}`, {
    method: 'PATCH',
    data,
    headers: {
      Action_Id: '200200004'
    }
  });
}

/** 获取商品规格 */
export async function getGoodsSpecsList(
  params: {
    current?: number;
    pageSize?: number;
    goods_id?: string;
    goods_dept_code?: string;
  }) {
  return request(`/api/inventory/goods/${params.goods_id}/specs/${params.current}/limit/${params.pageSize}?goods_dept_code=${params.goods_dept_code || ''}`, {
    method: 'GET'
  });
}

/** 上下架商品 */
export async function updateGoodsStauts(data: { goods_id?: string; goods_status?: string }) {
  return request(`/api/inventory/goods/${data.goods_id}/${data.goods_status}`, {
    method: 'PATCH',
    headers: {
      Action_Id: '200200002'
    }
  });
}

/** 获取检索码 */
export async function getSpell(data: any) {
  return request(`/api/inventory/spell?name=${data}`, {
    method: 'PATCH',
    data
  });
}

/** 品类管理接口 */
/** 获取品类列表 */
export async function getCategoryList(
  params: {
    current?: number;
    pageSize?: number;
    flag?: number;
    key_words?: number;
  }
) {
  return request(`/api/inventory/class/${params.current}/limit/${params.pageSize}`, {
    method: 'GET',
    params: {
      flag: params.flag,
      key_words: params.key_words
    },
    headers: {
      Action_Id: '200201000'
    }
  });
}

/** 获取品类列表(无需权限) */
export async function getCategoryListByPage(
  params: {
    current?: number;
    pageSize?: number;
  }
) {
  return request(`/api/inventory/class/page/${params.current}/limit/${params.pageSize}`, {
    method: 'GET'
  });
}

/** 新增品类 */
export async function doCreateCategory(data: STOCKGOODSAPI.CategoryItem) {
  return request('/api/inventory/class', {
    method: 'POST',
    headers: {
      Action_Id: '200201001'
    },
    data
  });
}

/** 批量删除品类 */
export async function deleteCategoryBatch(class_ids: string[]) {
  return request('/api/inventory/class/batch', {
    method: 'POST',
    headers: {
      Action_Id: '200201002'
    },
    data: {
      class_ids
    }
  });
}

/** 检查品类是否存在 */
export async function checkCategoryExist(params: any) {
  return request('/api/inventory/class/exist', {
    method: 'GET',
    params
  });
}

/** 获取品类详情 */
export async function getCategoryDetail(id: string) {
  return request(`/api/inventory/class/${id}`, {
    method: 'POST',
    headers: {
      Action_Id: '200201003'
    }
  });
}

/** 删除品类 */
export async function deleteCategory(class_id: string) {
  return request(`/api/inventory/class/${class_id}`, {
    method: 'DELETE',
    headers: {
      Action_Id: '200201005'
    }
  });
}

/** 更新品类 */
export async function updateCategory(data: any) {
  return request(`/api/inventory/class/${data.class_id}`, {
    method: 'PATCH',
    data,
    headers: {
      Action_Id: '200201004'
    }
  });
}

/** 查询库存调整列表(200203000) */
export async function getStockAdjustList(data: {
  begin_date?: string;
  end_date?: string;
  current?: number;
  pageSize?: number;
} & STOCKGOODSAPI.GoodsItem) {
  const { begin_date, end_date, current, pageSize } = data;
  return request(`/api/inventory/batch/approval/from/${current}/limit/${pageSize}`, {
    method: 'GET',
    params: {
      ...data
    },
    headers: {
      Action_Id: '200203000'
    }
  });
}

// 查询库存调整列表-下载(200203001)
export async function downStockAdjustList(data: {
  begin_date?: string;
  end_date?: string;
  current?: number;
  pageSize?: number;
} & STOCKGOODSAPI.GoodsItem) {
  const { begin_date, end_date, current, pageSize } = data;
  return request(`/api/inventory/batch/approval/report`, {
    method: 'GET',
    params: {
      ...data
    },
    headers: {
      Action_Id: '200203001'
    }
  });
}

/** 审批通过(200203004) */
export async function approveAccept(data: {
  approval_id: string;
  status: string;
  approve_explain: string;
}) {
  const { approval_id, status, approve_explain } = data;
  return request(`/api/inventory/batch/approval/${approval_id}/${status}`, {
    method: 'PATCH',
    data: {
      approve_explain
    },
    headers: {
      Action_Id: '200203004'
    }
  });
}

/** 审批拒绝(200203005) */
export async function approveReject(data: {
  approval_id: string;
  status: string;
  approve_explain: string;
}) {
  const { approval_id, status, approve_explain } = data;
  return request(`/api/inventory/batch/approval/${approval_id}/${status}`, {
    method: 'PATCH',
    data: {
      approve_explain
    },
    headers: {
      Action_Id: '200203005'
    }
  });
}

/** 批量审批通过/拒绝(200203003) */
export async function batchApprove(data: {
  approval_id: React.Key[];
  status: string;
  approve_explain: string;
}) {
  const { approval_id, status, approve_explain } = data;
  return request(`/api/inventory/batch/approval/${status}`, {
    method: 'PATCH',
    data: {
      approval_id_list: approval_id,
      approve_explain
    },
    headers: {
      Action_Id: '200203003'
    }
  });
}

/** 新增入库(200203002,200203006) */
export async function addStocks(data: any) {
  return request(`/api/inventory/batch/specs/${data.specs_id}`, {
    method: 'POST',
    data,
    headers: {
      Action_Id: '200203006'
    }
  });
}

/** 库存调整(200203002,200203007) */
export async function adjustStocks(data: any) {
  return request(`/api/inventory/batch/${data.batch_id}`, {
    method: 'POST',
    data,
    headers: {
      Action_Id: '200203007'
    }
  });
}

/** 批量库存调整 */
export async function batchAdjustStocks(data: any) {
  return request(`/api/inventory/batch/specs`, {
    method: 'POST',
    data
  });
}

/** 查询规格批次列表 */
export async function getSpecsBatchList(data: any) {
  return request(`/api/inventory/batch/${data.specs_id}/${data.current}/limit/${data.pageSize}?begin_date=${data.begin_date || ''}&end_date=${data.end_date || ''}`, {
    method: 'GET',
    data
  });
}
// 查询供应商列表
export async function getSupplierList() {
  return request(`/api/inventory/supplier/from/1/limit/999`);
}

// 获得中创商品列表
export async function getZcGoodList() {
  return request(`/api/inventory/zc/goods`);
}
// 获得中创商品规格列表
export async function getZcSpecsList(id: any) {
  return request(`/api/inventory/zc/goods/${id}/specs`);
}